package com.feihong.txgw.tx_precenter;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.feihong.txgw.inter.contract.kaika.IKaikaSubmitContract;
import com.feihong.txgw.json.kaika.KaiKaSubmitOrder;
import com.feihong.txgw.tx_model.KaikaSubmitMainModel;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.MultipartBody;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName KaikaSubmitMainPrecenter
 * @package com.feihong.txgw.tx_precenter
 * @data 2018/5/4 16:58
 * @Describe
 * @Change
 */
public class KaikaSubmitMainPrecenter extends IKaikaSubmitContract.AbKaikaSubmitMainPrecenter {

    public static KaikaSubmitMainPrecenter create(){
        return new KaikaSubmitMainPrecenter();
    }
    @Override
    public void submitNormalOrder(HashMap<String, String> mParams) {
        mIModel.submitNormaoOrder(mParams).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(String o) {
                KaiKaSubmitOrder kaiKaSubmitOrder = JSON.parseObject(o,KaiKaSubmitOrder.class);
                mIView.submitNormalOrderResult(kaiKaSubmitOrder);
            }

            @Override
            public void onError(Throwable e) {
                mIView.submitNormalOrderError();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void kaikaAuthResult(String json) {
        mIModel.kaikaAuthResult(json).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(String o) {
                mIView.kaikaAuthIDCardResult(o);
            }

            @Override
            public void onError(Throwable e) {
                mIView.kaikaAuthIDCardError();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void submitYuyueOrder(HashMap<String, String> mParams) {
        mIModel.submitYuyueOrder(mParams).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(String o) {
                mIView.submitYuyueOrderResult(o);
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                mIView.submitYuyueOrderError();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void kaikaUploadIDCardImg(MultipartBody.Part file) {
        mIModel.kaikaUploadIDCardImg(file).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(String o) {
                try {
                    mIView.dismissProgressDialog();
                    JSONObject jsonResult = new JSONObject(o);
                    if ("200".equals(jsonResult.optString("code"))) {
                        mIView.kaikaUploadIDCardImgResult(o);
                    } else {
                        mIView.showDialog(null, "请对身份证拍摄水平清晰图片");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable e) {
                mIView.dismissProgressDialog();
                mIView.showDialog(null,"请检查网络");
            }

            @Override
            public void onComplete() {
            }
        });
    }

    @Override
    public IKaikaSubmitContract.IKaikaSubmitMainModel getModel() {
        return KaikaSubmitMainModel.create();
    }

    @Override
    public void onStart() {

    }
}
